# Constructors for ProtocolApplication
# 
###############################################################################

#' Builds a ProtocolApplication object from a defining csv file.
#' 
#' A ProtocolApplication object is generated according to the specifications given in file.
#' For details on the file format see the drDobus vignette.
#' @param  file A character string naming the file that defines the protocolApplication
#' @export
#' @author Bastian R. Angermann
buildProtocolApplicationFromCsvFile <- function(file)
{
    theProtocolApplication <- new("ProtocolApplication")
    theProtocolApplication@workingDirectory <- setWorkingDirectory(file)
    file <- basename(file)
    protocolSettings <- readCsvSettings(file)
    # set and validate parameters
    theProtocolApplication@name <- file
    # TODO read reporting settings
    theProtocolApplication@report <- reporting:::defaultReport()#TODO get reporting
    
    if (!is.null(protocolSettings$childProtocol))
    {  
        for (protocolApplication in protocolSettings$childProtocol)
        {
            oldWorkingDirectory <- getwd()
            theChildProtocolApplication <- buildProtocolApplicationFromCsvFile(protocolApplication)
            theChildProtocolApplication@report <- theProtocolApplication@report # does not propagate to grand children FIXME
            theChildProtocolApplication@.parent <- theProtocolApplication
            theProtocolApplication@.childProtocolApplications <- append(theProtocolApplication@.childProtocolApplications,theChildProtocolApplication)
            setWorkingDirectory(oldWorkingDirectory)
        }
    }
    if (!is.null(protocolSettings$dataLocationFile)) 
    {
        theProtocolApplication@.dataLocationFile <- protocolSettings$dataLocationFile
      #  theProtocolApplication <- .setInputData(theProtocolApplication,theProtocolApplication@.dataLocationFile) #moved to execute since the input data might no be available at construction time if this is part of a mulit protocol pipeline
    }    
    if (!is.null(protocolSettings$ignoreList))
    {
        theProtocolApplication@ignoreList <- readCsvFile(protocolSettings$ignoreList)                    
    }    
   
    
    theProtocolApplication <- .setParameterValuesFromCsvFile(theProtocolApplication,protocolSettings$parameterFile)
    theProtocolApplication <- .setOutputData(theProtocolApplication,protocolSettings$outputDataLocation)
    theProtocolApplication <- .setAction(theProtocolApplication,protocolSettings$action)
    theProtocolApplication
}

#' Directly create a protocolApplication without a specification file.
#' 
#' Creates a protocolApplication from its arguments, not specification file is needed.
#' This function should only be used during testing of newly developed actions. It's not meant to
#' be used in a production environment.
#' @param parameterValues A named list of parameters passed to the action.
#' @param dataLocation A character string giving the location of the file specifying the input data.
#' @param actionName Name of a method with ProtocolApplication as first type in its signature.
#' @param name A character string giving the name of the protocolApplication
#' @param outputData A character string specifying the path to which output data should be written.
#' @author Bastian R. Angermann  
#' @export 
buildProtocolApplication <- function( parameterValues, dataLocation, actionName,name="Test", outputData=NULL)
{
    theProtocolApplication <- new("ProtocolApplication")
    theProtocolApplication@name <- name
    theProtocolApplication@parameterValues <- parameterValues

    theProtocolApplication@workingDirectory <- setWorkingDirectory(getwd())
    theProtocolApplication@report <- reporting:::defaultReport()
    if (! is.null(outputData))  
    { 
        theProtocolApplication@outputData <- outputData
    }
    
    theProtocolApplication@.dataLocationFile <- dataLocation

    
    theProtocolApplication <- .setAction(theProtocolApplication,actionName)
    
    #submitToLog("buildProtocolApplication created a ProtocolApplication. This should never happen in Production.",
    #            "Warning")
    theProtocolApplication
}

#' Build a ProtocolApplication form its XML representation
#' 
#' @param fileName A character string specifying the name of an xml protocol application file
buildProtocolApplicationFromXml <- function(fileName)
{
    xmlDocument <- readXML(fileName)
    validateXml(xmlDocument)
    
    # now that we know we have a valid Pipeline representation parse it into a ProtocolApplication
    # Action nodes are ignored as we are not concerned with action definitions.
    
    # as a prototype we only parse ActionApplications and ignore pipelines of now.  
    
    root <- xmlRoot(xmlDocument)
    for ( child in xmlChildren(root) )
        print(child)
}

